<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ name: 'home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品分类</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card class="content">
      <!-- 头部功能 -->
      <template #header>
        <div>
          <el-button type="primary" size="medium"
            >添加分类</el-button
          >
        </div>
      </template>
      <!-- 正文内容 -->
      <el-table :data="types" style="width: 100%" border size="small" v-loading="loading"
        row-key="cat_id"
        :tree-props="{children: 'children'}"
      >
        <template #empty>
          <el-empty description="暂无数据"></el-empty>
        </template>
        <el-table-column
          type="index"
          label="序号"
          width="60px"
        ></el-table-column>
        <el-table-column prop="cat_name" label="分类名称"> </el-table-column>
        <el-table-column label="是否有效" #default="{ row }">
          <i class="tip" :class=" row.cat_deleted?'el-icon-error':'el-icon-success' "></i>
        </el-table-column>
        <el-table-column label="级别" #default="{ row }">
          <el-tag v-show="row.cat_level==0" >一级</el-tag>
          <el-tag v-show="row.cat_level==1" type="success">二级</el-tag>
          <el-tag v-show="row.cat_level==2" type="warning">三级</el-tag>
        </el-table-column>
        <el-table-column label="操作" width="200px" #default="{ row }">
          <el-button type="primary" size="mini" icon="el-icon-edit">编辑</el-button>
          <el-button
            type="danger"
            size="mini"
            icon="el-icon-delete"
            @click="removeTypeById(row.cat_id)"
          >删除</el-button>
        </el-table-column>
      </el-table>
      <!-- 分页器 -->
      <el-pagination background layout="total, sizes, prev, pager, next, jumper"
        :page-sizes="[5,10,15,20]"
        :page-size="pagesize"
        :total="total"
        :current-page="pagenum"
        @current-change="reloadTypelistPage"
        @size-change="reloadTypelistSize"
      >
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    return {
      loading:false,
      total:0,
      pagesize:5,
      pagenum:1,
      query:"",
      types:[]
    };
  },
  methods: {
    reloadTypelistPage(newPageNum){
      // console.log(this.pagenum,newPageNum);
      this.pagenum = newPageNum;
      this.loadTypeListPage();
    },
    reloadTypelistSize(newPageSize){
      // console.log(this.pagesize,newPageSize);
      this.pagenum = 1;  // 每页条数发生变化，表示分页数量也将发生变化，所以需要重置页面访问
      this.pagesize = newPageSize;
      this.loadTypeListPage();
    },
    async loadTypeListPage() {
      try {
        this.loading = true;
        let result = await this.$api.getCategories({
          pagenum:this.pagenum,
          pagesize:this.pagesize
        })
        if (result == "error") return;
        this.total = result.total;
        this.pagenum = result.pagenum + 1;
        this.types = result.result;
      } catch (error) {
        console.log("CategoriesView-loadTypeListPage-error:", error);
      } finally {
        this.loading = false;
      }
    },
    async removeTypeById(id) {
      try {
        console.log(id);
        // if (result == "error") return;
        // this.$message.success("删除成功");
        // let lastFlag = this.total%this.pagesize==1;
        // let lastPageFlag = Math.ceil(this.total/this.pagesize)==this.pagenum;
        // if(lastFlag&&lastPageFlag&&this.pagenum!=1){
        //   this.pagenum = this.pagenum - 1;
        // }
        // this.loadTypeListPage();
      } catch (error) {
        console.log("CategoriesView-removeTypeById-error:", error);
      }
    }
  },
  created() {
    this.loadTypeListPage();
  },
};
</script>


<style lang="less" scoped>
.content,.el-pagination{
  margin-top: 20px;
}
.search-input {
  width: 300px;
}
.add-btn {
  margin-left: 20px;
}
.tip{
  font-size: 18px;
}
.tip.el-icon-error{
  color: red;
}
.tip.el-icon-success{
  color: green;

}
</style>